<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/11 0011
 * Time: 上午 9:18
 */

namespace app\admin\controller;
use app\admin\model\Menu;
use think\Request;

class MenuManage extends Common
{
    /**
     * @title 栏目列表
     */
    public function index()
    {
        $menu = new Menu();
        $list = $menu::all(function($query){
            $query->order('sort asc');
        });
        $this->assign('list',getMenu($list->toarray()));
//        dump(getMenu($list->toarray()));die;
        return $this->fetch('admin/menumanage/menu_list');
    }

    /**
     * @title 修改栏目显示页
     * @param null $id
     * @return mixed
     */
    public function edit(&$id=null)
    {
        $menu = new Menu();
        $get = $menu::get($id)->toArray();
        $this->assign('res',$get);
        $select = $menu::all(function($query){
            $query->field('id,pid,title');
        });
        $this->assign('select',getMenu($select->toarray()));
        return $this->fetch('admin/menumanage/edit');
    }

    /**
     * @title   添加栏目显示页
     * @return mixed
     */
    public function add()
    {
        $menu = new Menu();
        $select = $menu::all(function($query){
            $query->field('id,pid,title');
        });
//        dump(getMenu($select->toarray()));die;
        $this->assign('select',getMenu($select->toarray()));
        return $this->fetch('admin/menumanage/add');
    }

    /**
     * @title   添加修改操作
     */
    public function update()
    {
        if(Request::instance()->isAjax()) {
            $data = Request::instance()->param();
            $data['display'] = isset($data['display'])?$data['display']:'0';
            $res = $this->store(new Menu(), $data);
            if ($res['status'] == 'success') {
                $this->success($res['msg'], 'index');
            } else {
                $this->error('操作失败');
            }
        }
    }

    public function del(&$id = null)
    {
        $menu = new Menu();
        $del = $menu::get($id);
        if($del->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}